package com.one.controller;

import com.one.annotation.RequestPermission;
import com.one.annotation.RequestUser;
import com.one.service.UserIntegralService;
import com.one.util.RedisUtils;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.UserVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;

/**
  * @author : zzh
  * @date : 2022/1/14
  * @return :
  * @description : 用户积分控制层
 */
@RestController
@Slf4j
@RequestMapping("/Integral")
public class UserIntegralController {

    @Autowired
    private UserIntegralService userIntegralService;

    @Autowired
    private RedisUtils redisUtils;

    /**
      * @author : zzh
      * @date : 2022/1/14
      * @return :
      * @description : 根据用户花费金额增加用户现有积分
     */
    @GetMapping("/updateIntegral")
    @RequestPermission
    public RequestReturn<HashMap<String, Object>> updateUserTicket(@RequestParam Integer payment, @RequestUser UserVo userVo, @RequestHeader String token){
        // TODO 用户商品购买完成

        HashMap<String, Object> maps = new HashMap<>();
        //根据价格计算获得积分
        Integer integral = payment;
        log.info("------id为{}的用户，获得积分{}",userVo.getUserId(),integral);
        //用户积分总和
        Integer integralSum = integral + userVo.getIntegral();
        userVo.setIntegral(integralSum);
        //传入redis
        redisUtils.set(token,userVo);
//        log.info("------id为{}的用户花费了:{}金币,总积分为:{}------",userVo.getUserId(),payment,integralSum);
        int i = userIntegralService.updateUserIntegral(userVo.getUserId(), integralSum);
//        maps.put("expenditure",payment);
        maps.put("getPoint",integral);
        maps.put("accountOfIntegral",integralSum);
        return RequestReturnFactory.success(maps);
    }


}
